package com.lleice.service.service.sys.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lleice.common.utils.ChineseCharToEn;
import com.lleice.service.entity.sys.SysRole;
import com.lleice.service.entity.sys.SysRoleResource;
import com.lleice.service.mapper.sys.SysRoleMapper;
import com.lleice.service.qo.other.BatchDeleteQo;
import com.lleice.service.qo.sys.ResoucesAuthorizationReq;
import com.lleice.service.qo.sys.RoleReq;
import com.lleice.service.service.sys.ISysRoleResourceService;
import com.lleice.service.service.sys.ISysRoleService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lizihao
 * @since 2020-11-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
	
	@Autowired
	private ISysRoleResourceService iSysRoleResourceService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdateUser(RoleReq roleReq) throws Exception {
		if(null == roleReq.getId()) {
			//新增
			SysRole role = new SysRole().
					setRoleName(roleReq.getRoleName()).
					setSortNum(this.baseMapper.getMaxOrderNum()+1)
					.setSortNum(roleReq.getSortNum());
			if(StrUtil.isBlank(roleReq.getRoleCode())) {
				role.setRoleCode(ChineseCharToEn.getAllFirstLetter(roleReq.getRoleName()));
			}
			this.baseMapper.insert(role);
		}else {
			SysRole role = new SysRole();
			role.setId(Long.valueOf(roleReq.getId()));
			role.setRoleName(roleReq.getRoleName());
			role.setSortNum(roleReq.getSortNum());
			role.setIsEnable(roleReq.getIsEnable());
			this.baseMapper.updateById(role);
		}
		
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void resoucesAuthorization(ResoucesAuthorizationReq resoucesAuthorizationReq) throws Exception {
				//先删除角色的授权
				QueryWrapper<SysRoleResource> queryWrapper = new QueryWrapper<>();
		        queryWrapper.eq("role_id", resoucesAuthorizationReq.getRoleId());
		        iSysRoleResourceService.remove(queryWrapper);
		        //再授权
		       List<SysRoleResource> insertList =  resoucesAuthorizationReq
		    		   .getResoucesList()
		    		   .stream()
		    		   .map(a -> new SysRoleResource().setResourceId(Long.valueOf(a))
		    				   .setRoleId(Long.valueOf(resoucesAuthorizationReq.getRoleId())))
		    		   .collect(Collectors.toList());
		        iSysRoleResourceService.saveBatch(insertList);
		
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(BatchDeleteQo roleBatchDeleteDto) {
		roleBatchDeleteDto.getIds().forEach(a ->{
			SysRole role = new SysRole();
			role.setId(Long.valueOf(a));
			role.setIsDelete(1);
			this.baseMapper.updateById(role);
		});
		
	}

}
